package com.kiven.beanutils;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.Map.Entry;

import org.apache.log4j.Logger;

/**
 * @author kiven
 *
 */
public class SystemConfig {
	public static boolean IS_LOCAL = false;
	private static SystemConfig me;
	private static ConfigXmlVO vo;
	public static String PROVIDER_URL = "";
	public static String INITIAL_CONTEXT_FACTORY = "";
	public static String URL_PKG_PREFIXES = "";
	public static String PROVIDER_FACTORY = "";
	public static String REDHAT_JBOSS = "";
	public static String SECURITY_AUTHENTICATION = "";
	public static String SECURITY_PRINCIPAL = "";
	public static String SECURITY_CREDENTIALS = "";
	public static String SECURITY_PROTOCOL = "";
	private static String config = "resource.plt.config.SystemConfig";
	private static String OM_EDITION = "";
	private static Logger logger = Debug.getLogger();
	private static HashMap<Integer, Properties> hm;
	private static Properties dbProp;
	private static String SERVERNAME;
	private static int DB_TIMEOUT = 30;
	private static String Ticket = null;
	private static boolean isTicketLogin = false;
	private static boolean isSetTicketDone = false;
	
	public static boolean setTicket(String paramString, boolean paramBoolean) {
		if (!isSetTicketDone) {
			Ticket = paramString;
			isTicketLogin = paramBoolean;
			isSetTicketDone = true;
			return true;
		}
		return false;
	}
	
	public static String getTicket() {
		return Ticket;
	}

	public static boolean isTicketLogin() {
		return isTicketLogin;
	}
	
	public static HashMap<Integer, Properties> getServerProperties() {
		if (hm == null)
			hm = new HashMap();
		try {
			loadDBProp();
			String str = dbProp.getProperty("REDHAT_JBOSS");
			String[] arrayOfString1 = dbProp.getProperty(
					"INITIAL_CONTEXT_FACTORY").split(";");
			String[] arrayOfString2 = null;
			if (dbProp.getProperty("URL_PKG_PREFIXES") != null)
				arrayOfString2 = dbProp.getProperty("URL_PKG_PREFIXES").split(
						";");
			String[] arrayOfString3 = dbProp.getProperty("PROVIDER_URL").split(
					";");
			String[] arrayOfString4 = dbProp.getProperty("PROVIDER_FACTORY")
					.split(";");
			Debug.getLogger("trace_url", false).info(
					new StringBuilder().append("URL=")
							.append(arrayOfString3[0]).toString());
			Debug.getLogger("trace_url", false).info(
					new StringBuilder().append("PROVIDER_URL=").append(
							PROVIDER_URL).toString());
			Object localObject;
			if (PROVIDER_URL.startsWith("t3")) {
				for (int i = 0; i < arrayOfString3.length; i++) {
					localObject = new Properties();
					if (i == 0) {
						((Properties) localObject)
								.put(
										"java.naming.factory.initial",
										INITIAL_CONTEXT_FACTORY.equals("") ? arrayOfString1[i]
												: INITIAL_CONTEXT_FACTORY);
						((Properties) localObject)
								.put(
										"java.naming.provider.url",
										new StringBuilder()
												.append(
														PROVIDER_URL.equals("") ? arrayOfString3[i]
																: PROVIDER_URL)
												.append(
														PROVIDER_FACTORY
																.equals("") ? arrayOfString4[i]
																: PROVIDER_FACTORY)
												.toString());
						Debug
								.getLogger("trace_url", false)
								.info(
										new StringBuilder()
												.append(
														"Context.INITIAL_CONTEXT_FACTORY=")
												.append(
														((Properties) localObject)
																.getProperty("java.naming.factory.initial"))
												.toString());
						Debug
								.getLogger("trace_url", false)
								.info(
										new StringBuilder()
												.append("Context.PROVIDER_URL=")
												.append(
														((Properties) localObject)
																.getProperty("java.naming.provider.url"))
												.toString());
					} else {
						((Properties) localObject).put(
								"java.naming.factory.initial",
								arrayOfString1[i]);
						((Properties) localObject).put(
								"java.naming.provider.url", new StringBuilder()
										.append(arrayOfString3[i]).append(
												arrayOfString4[i]).toString());
						Debug
								.getLogger("trace_url", false)
								.info(
										new StringBuilder()
												.append(
														"Context.INITIAL_CONTEXT_FACTORY=")
												.append(
														((Properties) localObject)
																.getProperty("java.naming.factory.initial"))
												.toString());
						Debug
								.getLogger("trace_url", false)
								.info(
										new StringBuilder()
												.append("Context.PROVIDER_URL=")
												.append(
														((Properties) localObject)
																.getProperty("java.naming.provider.url"))
												.toString());
					}
					Debug.getLogger("trace_url", false).info(
							new StringBuilder().append("url lenght >> i = ")
									.append(i).toString());
					hm.put(Integer.valueOf(i), (Properties)localObject);
				}
			} else if ((str != null) && (str.equalsIgnoreCase("Y"))) {
				String[] arrayOfString5 = null;
				localObject = null;
				String[] arrayOfString6 = null;
				String[] arrayOfString7 = null;
				String[] arrayOfString8 = null;
				if (dbProp.getProperty("SECURITY_AUTHENTICATION") != null)
					arrayOfString5 = dbProp.getProperty(
							"SECURITY_AUTHENTICATION").split(";");
				if (dbProp.getProperty("SECURITY_PRINCIPAL") != null)
					localObject = dbProp.getProperty("SECURITY_PRINCIPAL")
							.split(";");
				if (dbProp.getProperty("SECURITY_CREDENTIALS") != null)
					arrayOfString6 = dbProp.getProperty("SECURITY_CREDENTIALS")
							.split(";");
				if (dbProp.getProperty("SECURITY_PROTOCOL") != null)
					arrayOfString7 = dbProp.getProperty("SECURITY_PROTOCOL")
							.split(";");
				
				if (localObject != null) 
					arrayOfString8 = (String[]) localObject;
				
				for (int k = 0; k < arrayOfString3.length; k++) {
					Properties localProperties = new Properties();
					if ((arrayOfString7 != null)
							&& (!arrayOfString7[k].trim().equals("")))
						localProperties
								.put(
										"java.naming.security.protocol",
										SECURITY_PROTOCOL.equals("") ? arrayOfString7[k]
												: SECURITY_PROTOCOL);
					if (k == 0) {
						localProperties
								.put(
										"java.naming.factory.initial",
										INITIAL_CONTEXT_FACTORY.equals("") ? arrayOfString1[k]
												: INITIAL_CONTEXT_FACTORY);
						if ((!URL_PKG_PREFIXES.equals(""))
								|| (arrayOfString2 != null))
							localProperties
									.put(
											"java.naming.factory.url.pkgs",
											URL_PKG_PREFIXES.equals("") ? arrayOfString2[k]
													: URL_PKG_PREFIXES);
						localProperties
								.put(
										"java.naming.provider.url",
										new StringBuilder()
												.append(
														PROVIDER_URL.equals("") ? arrayOfString3[k]
																: PROVIDER_URL)
												.append(
														PROVIDER_FACTORY
																.equals("") ? arrayOfString4[k]
																: PROVIDER_FACTORY)
												.toString());
						Debug
								.getLogger("trace_url", false)
								.info(
										new StringBuilder()
												.append(
														" JBoss Context.PROVIDER_URL=")
												.append(
														localProperties
																.getProperty("java.naming.provider.url"))
												.toString());
						if ((arrayOfString5 != null)
								&& (!arrayOfString5[k].trim().equals("")))
							localProperties
									.put(
											"java.naming.security.authentication",
											SECURITY_AUTHENTICATION.equals("") ? arrayOfString5[k]
													: SECURITY_AUTHENTICATION);
						if ((localObject != null)
								&& (!arrayOfString8[k].trim().equals("")))
							localProperties
									.put(
											"java.naming.security.principal",
											SECURITY_PRINCIPAL.equals("") ? arrayOfString8[k]
													: SECURITY_PRINCIPAL);
						if ((arrayOfString6 != null)
								&& (!arrayOfString6[k].trim().equals("")))
							localProperties
									.put(
											"java.naming.security.credentials",
											SECURITY_CREDENTIALS.equals("") ? arrayOfString6[k]
													: SECURITY_CREDENTIALS);
					} else {
						localProperties.put("java.naming.factory.initial",
								arrayOfString1[k]);
						if (arrayOfString2 != null)
							localProperties.put("java.naming.factory.url.pkgs",
									arrayOfString2[k]);
						localProperties.put("java.naming.provider.url",
								new StringBuilder().append(arrayOfString3[k])
										.append(arrayOfString4[k]).toString());
						Debug
								.getLogger("trace_url", false)
								.info(
										new StringBuilder()
												.append(
														" JBoss Context.PROVIDER_URL=")
												.append(
														localProperties
																.getProperty("java.naming.provider.url"))
												.toString());
						if ((arrayOfString5 != null)
								&& (!arrayOfString5[k].trim().equals("")))
							localProperties.put(
									"java.naming.security.authentication",
									arrayOfString5[k]);
						if ((localObject != null)
								&& (!arrayOfString8[k].trim().equals("")))
							localProperties.put(
									"java.naming.security.principal",
									arrayOfString8[k]);
						if ((arrayOfString6 != null)
								&& (!arrayOfString6[k].trim().equals("")))
							localProperties.put(
									"java.naming.security.credentials",
									arrayOfString6[k]);
					}
					Debug.getLogger("trace_url", false).info(
							new StringBuilder().append(
									"jboss url lenght >> i = ").append(k)
									.toString());
					hm.put(Integer.valueOf(k), localProperties);
				}
			} else if ((str == null) || (str.equalsIgnoreCase("N"))) {
				for (int j = 0; j < arrayOfString3.length; j++) {
					localObject = new Properties();
					if (j == 0) {
						((Properties) localObject)
								.put(
										"java.naming.factory.initial",
										INITIAL_CONTEXT_FACTORY.equals("") ? arrayOfString1[j]
												: INITIAL_CONTEXT_FACTORY);
						((Properties) localObject).put(
								"java.naming.factory.url.pkgs",
								URL_PKG_PREFIXES.equals("") ? arrayOfString2[j]
										: URL_PKG_PREFIXES);
						((Properties) localObject)
								.put(
										"java.naming.provider.url",
										new StringBuilder()
												.append(
														PROVIDER_URL.equals("") ? arrayOfString3[j]
																: PROVIDER_URL)
												.append(
														PROVIDER_FACTORY
																.equals("") ? arrayOfString4[j]
																: PROVIDER_FACTORY)
												.toString());
						Debug
								.getLogger("trace_url", false)
								.info(
										new StringBuilder()
												.append(
														" JBoss =N  Context.PROVIDER_URL=")
												.append(
														((Properties) localObject)
																.getProperty("java.naming.provider.url"))
												.toString());
					} else {
						((Properties) localObject).put(
								"java.naming.factory.initial",
								arrayOfString1[j]);
						((Properties) localObject).put(
								"java.naming.factory.url.pkgs",
								arrayOfString2[j]);
						((Properties) localObject).put(
								"java.naming.provider.url", new StringBuilder()
										.append(arrayOfString3[j]).append(
												arrayOfString4[j]).toString());
						Debug
								.getLogger("trace_url", false)
								.info(
										new StringBuilder()
												.append(
														" JBoss =N Context.PROVIDER_URL=")
												.append(
														((Properties) localObject)
																.getProperty("java.naming.provider.url"))
												.toString());
					}
					hm.put(Integer.valueOf(j), (Properties)localObject);
				}
			}
		} catch (IOException localIOException) {
			localIOException.printStackTrace();
			logger.error(localIOException);
		}
		return (HashMap<Integer, Properties>) hm;
	}
	
	private static void loadDBProp() throws IOException {
		int i = 1;
		try {
			if ((dbProp == null) || (dbProp.size() == 0)) {
				dbProp = new Properties();
				dbProp.load(new FileInputStream("SystemConfig.properties"));
				Debug.getLogger("trace_url", false).info(
						new StringBuilder().append(
								"dbProp.INITIAL_CONTEXT_FACTORY=").append(
								dbProp.getProperty("INITIAL_CONTEXT_FACTORY"))
								.toString());
				Debug.getLogger("trace_url", false).info(
						new StringBuilder().append("dbProp.PROVIDER_URL=")
								.append(dbProp.getProperty("PROVIDER_URL"))
								.toString());
			}
		} catch (IOException localIOException) {
			localIOException.printStackTrace();
			logger.error(localIOException);
			throw localIOException;
		}
	}
	
//	public static void main(String[] args) {
//		Map<Integer,Properties> localMap = SystemConfig.getServerProperties();
//		
//		Set<Entry<Integer,Properties>> set = localMap.entrySet();
//		
//		Iterator<Entry<Integer, Properties>> iter = set.iterator();
//		while(iter.hasNext()) {
//			Entry entry = iter.next();
//			System.out.println("Key = " + entry.getKey());
//			System.out.println("Value = " + entry.getValue());
//		}
//	}
}